﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HCMServices.DTO;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace HCMServices.DAO
{
    /// <summary>
    /// This class process for DICHVU Table
    /// </summary>
    public class ServiceDAO
    {
        private MyDataContext db;

        /// <summary>
        /// Constructor of class
        /// </summary>
        /// <param name="connectString">String indentify how to connect database</param>
        public ServiceDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        /// <summary>
        /// This function search DICHVU row base on ID Column 
        /// </summary>
        /// <param name="id">ID to find</param>
        /// <returns>DICHVU row if id was found or null</returns>
        public ServiceDTO Search(int id)
        {
            ServiceDTO dichVuDTO = new ServiceDTO();

            IList<DICHVU> query = (from DICHVU dv in db.Service
                                   where dv.MaDichVu == id
                                   select dv).ToList();

            foreach (DICHVU dv in query)
            {
                dichVuDTO.Id = dv.MaDichVu;
                dichVuDTO.Name = dv.TenDichVu;

                return dichVuDTO;              
            }

            return dichVuDTO;
        }

        /// <summary>
        /// This function gets all DICHVU rows in database
        /// </summary>
        /// <returns>List of DICHVU rows</returns>
        public List<ServiceDTO> GetAllDichVuDTO()
        {
            List<ServiceDTO> lstDichVuDTO = new List<ServiceDTO>();

            IList<DICHVU> query = (from DICHVU dv in db.Service
                                   select dv).ToList();

            foreach (DICHVU dv in query)
            {
                ServiceDTO temp = new ServiceDTO();
                temp.Id = dv.MaDichVu;
                temp.Name = dv.TenDichVu;

                lstDichVuDTO.Add(temp);
            }

            return lstDichVuDTO;
        }

        /// <summary>
        /// This function retrieve id of DICHVU base on TenDichVu
        /// </summary>
        /// <param name="name">String of TenDichVu want to find</param>
        /// <returns>MaDichVu of DICHVU was found or -1</returns>
        public int GetIdByName(string name)
        {
            int maDichVu = -1;

            IList<DICHVU> query = (from DICHVU dv in db.Service
                                   where dv.TenDichVu == name
                                   select dv).ToList();

            foreach (DICHVU dv in query)
            {
                maDichVu = dv.MaDichVu;
                return maDichVu;                
            }

            return maDichVu;                    
        }
    }
}